package com.hxd.otc.module.home.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.hxd.otc.R;
import com.hxd.otc.base.BaseRecyclerActivity;
import com.hxd.otc.module.home.adapter.TicketDetailAdapter;
import com.hxd.otc.module.home.contract.TicketDetailContract;
import com.hxd.otc.module.home.presenter.TicketDetailPresenter;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.utils.hyphenate.IListener;
import com.hxd.otc.utils.hyphenate.ListenerManager;
import com.hxd.otc.widget.TitleBar;
import com.hyphenate.helpdesk.domain.CommentEntity;
import com.hyphenate.helpdesk.domain.TicketEntity;
import com.hyphenate.helpdesk.easeui.UIProvider;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

/**
 * Created by lzy on 2019/1/16 0016.
 * description ${留言详情}
 */
public class TicketDetailActivity extends BaseRecyclerActivity<TicketDetailPresenter, TicketDetailAdapter, CommentEntity> implements TicketDetailContract.IView, IListener {
    @BindView(R.id.title_bar)
    TitleBar mTitleBar;

    /**
     * loading等待dialog
     */
    private ProgressDialog pd;

    private static final int REQUEST_CODE_NEW_COMMENT = 2001;

    @Override
    protected TicketDetailAdapter createAdapter() {
        return new TicketDetailAdapter(this);
    }

    @Override
    protected TicketDetailPresenter createPresenter() {
        return new TicketDetailPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ticket_detail;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ListenerManager.getInstance().registerListener(this);
    }

    @Override
    protected void initView() {
        mPresenter.setTicketEntity((TicketEntity) getIntent().getParcelableExtra("ticket"));
        super.initView();
        mAdapter.setHeaderData(mPresenter.getTicketEntity());
        mPresenter.onRefresh();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mTitleBar.setOnTitleBarItemClickListener(new TitleBar.OnTitleBarItemClickListener() {
            @Override
            public void onBack(View v) {

            }

            @Override
            public void onRight(View v) {
                onReply();
            }
        });
    }

    private void onReply() {
        Intent intent = new Intent();
        intent.putExtra("id", mPresenter.getTicketEntity().getId());
        intent.setClass(TicketDetailActivity.this, NewCommentActivity.class);
        startActivityForResult(intent, REQUEST_CODE_NEW_COMMENT);
    }

    @Override
    protected void onResume() {
        super.onResume();
        UIProvider.getInstance().getNotifier().reset();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ListenerManager.getInstance().unRegisterListener(this);
        UIUtils.dismissDialog(pd);
    }

    @Override
    public void runOnUi(Runnable runnable) {
        runOnUiThread(runnable);
    }

    @Override
    public void notifyEvent(String eventStr, Object eventObj) {
        if (!TextUtils.isEmpty(eventStr) && eventStr.equals("CommentCreatedEvent")) {
            // 评论发生改变
            if (eventObj != null && eventObj instanceof JSONObject) {
                JSONObject ticketJson = (JSONObject) eventObj;
                try {
                    long ticketId = ticketJson.getLong("id");
                    if (mPresenter.getTicketEntity().getId().equals(String.valueOf(ticketId))) {
                        mPresenter.onRefresh();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_NEW_COMMENT) {
            // 添加了新的评论,刷新列表
            mPresenter.onRefresh();
        }
    }

//    @Override
//    public void openLocalFile(File file) {
//        Intent intent = new Intent();
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        //设置intent的Action属性
//        intent.setAction(Intent.ACTION_VIEW);
//        //获取文件file的MIME类型
//        String type = CommonUtil.getMIMEType(file);
//        //设置intent的data和Type属性。
//        intent.setDataAndType(/*uri*/Uri.fromFile(file), type);
//        //跳转
//        try {
//            startActivity(intent); //这里最好try一下，有可能会报错。 //比如说你的MIME类型是打开邮箱，但是你手机里面没装邮箱客户端，就会报错。
//        } catch (Exception e) {
//            showToast(getString(R.string.file_cannot_be_opened));
//        }
//    }
//
//    @Override
//    public void downloadFile(String remoteUrl, String localName, String fileType) {
////        Intent intent = new Intent();
////        intent.setClass(getBaseContext(), FileDownloadActivity.class);
////        intent.putExtra("remoteUrl", remoteUrl);
////        intent.putExtra("localName", localName);
////        intent.putExtra("type", fileType);
////        startActivity(intent);
//    }


}
